﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Xml;
using ProgNetComponentsDx.Controls.Editors;
using DevExpress.XtraEditors.Repository;
using System.Drawing.Design;
using ProgNetComponentsDx.Chat;

namespace WindowsFormsApplication1
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Form1 : XtraForm
    {
        public Form1()
        {
            InitializeComponent();
            
            serwer.MessageReceived += new ChatMessageHandler(serwer_MessageReceived);
            listBoxControl1.DataSource = new BindingList<string>(serwer.Users);

            client1.MessageReceived += new ChatMessageHandler(client1_MessageReceived);
            client2.MessageReceived += new ChatMessageHandler(client2_MessageReceived);
        }

        void client2_MessageReceived(object sender, ChatMessageEventArgs e)
        {
            this.memoEdit1.AppendLine(client2.Nick + " otrzymal wiadomosc : " + e.Info.ToString());
        }

        void client1_MessageReceived(object sender, ChatMessageEventArgs e)
        {
            this.memoEdit1.AppendLine(client1.Nick + " otrzymal wiadomosc : " + e.Info.ToString());
        }

        void serwer_MessageReceived(object sender, ChatMessageEventArgs e)
        {
            this.memoEdit1.AppendLine("Serwer: otrzymal wiadomosc od " + sender+ " : " + e.Info.ToString());
        }

        ChatServer serwer = new ChatServer(8099);
        MyChatClient client1 = new MyChatClient("pcplukom-1", 8099);
        MyChatClient client2 = new MyChatClient("pcplukom-1", 8099);

        private void btnStartServer_Click(object sender, EventArgs e)
        {
            serwer.Start();
        }

        private void btnStartClient_Click(object sender, EventArgs e)
        {
            client1.StartOrWait();
        }

        private void btnStartClient2_Click(object sender, EventArgs e)
        {
            client2.StartOrWait();
        }

        private void btnStopSerwer_Click(object sender, EventArgs e)
        {
            serwer.Stop();
        }

        private void btnStopClient_Click(object sender, EventArgs e)
        {
            client1.Stop();
        }

        private void btnStopClient2_Click(object sender, EventArgs e)
        {
            client2.Stop();
        }

        private void btnCl1SendToSerwer_Click(object sender, EventArgs e)
        {
            client1.SendMessageToServer("somemsgtype", "jakas wiadomosc");
        }

        private void btnCl1SendToClient2_Click(object sender, EventArgs e)
        {
            client1.SendMessage(client2.Nick, "jakas wiadomosc do cl 2");
        }

        private void btnCl2SendToSerwer_Click(object sender, EventArgs e)
        {
            client2.SendMessageToServer("somemsgtype2", "jakas wiadomosc od cl2");
        }

        private void btnCl2SendToClient1_Click(object sender, EventArgs e)
        {
            client2.SendMessage(client1.Nick, "jakas wiadomosc do cl 1");
        }
    }
}
